<?php


namespace app\article\controller;

use app\article\model\ArticleClassifyModel;
use app\AdminRest;
use app\article\model\ArticleModel;
use think\App;


class AdminArticleClassify extends AdminRest
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    public function listAll()
    {
        $param = $this->_param;
        $page_config = array(
            'page' => 1,
            'page_count' => 20
        );
        //设置页码
        if (isset($param['page']) && $param['page'] > 0) $page_config['page'] = $param['page'];
        //设置每页的数据
        if (isset($param['page_count']) && $param['page_count'] > 0) $page_config['page_count'] = $param['page_count'];
        $filter[] = ['uniacid','=',$this->_uniacid];
        $filter[] = ['status','>',-1];
        $model = new ArticleClassifyModel();
        $page_config['total'] = $model->countByFilter($filter);
        $rsts = $model->getListByFilter($filter, $page_config);
        $page_config['total_page'] = (int)($page_config['total'] / $page_config['page_count']);
        if (($page_config['total'] % $page_config['page_count']) > 0) $page_config['total_page'] = $page_config['total_page'] + 1;
        //设置返回参数
        $result = $page_config;
        //返回数据
        $result['data'] = $rsts;
        return $this->success($result);
    }

    public function listAllForSelect()
    {
        $filter = [
            ['uniacid', '=', $this->_uniacid],
            ['status', '=', 1],
        ];
        $model = new ArticleClassifyModel();
        $rsts = $model->field('id, title')->where($filter)
            ->order('top desc,id desc')
            ->select();

        return $this->success($rsts);

    }
    public function createOne()
    {
        $input = $this->_input;
        unset($input['id']);
        $title   = $input['title'] ?? null;
        if (!$title&&$title!=0)
        {
            return $this->error('title is required');
        }
        $input['uniacid'] = $this->_uniacid;
        $model = new ArticleClassifyModel();

        if ($model->where(['title' => $input['title']])->count())
        if ($model->where([['title', '=', $input['title']], ['uniacid', '=', $this->_uniacid],['status', '>', '-1']])->count())
        {
            return $this->error('this title is exists');
        }

        $res = $model->createOne($input);
        return $this->success($res);
    }

    public function updateOne()
    {
        $id   = $this->_param['id'] ?? null;
        if (!$id)
        {
            return $this->error('classify_id is required');
        }
        $input = $this->_input;
        $model = new ArticleClassifyModel();

        if ($model->where([
            ['id', '<>', $id],
            ['title', '=', $input['title']],
            ['uniacid', '=', $this->_uniacid],
            ['status', '>', '-1']
        ])->count())
        {
            return $this->error('this title is exists');
        }


        $data = $model->updateByfilter(['id' => $id], $input);
        return $this->success($data);
    }

    public function getOne()
    {
        $classify_id   = $this->_param['id'] ?? null;
        if (!$classify_id)
        {
            return $this->error('classify_id is required');
        }
        $model = new ArticleClassifyModel();
        $data = $model->getOne(['id' => $classify_id]);
        return $this->success($data);
    }

    public function changeStatus()
    {
        $classify_id   = $this->_param['id'];
        if (!$classify_id)
        {
            return $this->error('classify_id is required');
        }
        $input = $this->_input;
        $model = new ArticleClassifyModel();

//        if ($input['status'] != 1 && ArticleModel::where([['classify_id', '=', $classify_id], ['status', '=', 1] ])->count())
//        {
//            return $this->error('this classify has article');
//        }


        $rst = $model->updateByfilter(['id' => $classify_id], ['status' => $input['status']]);
        return $this->success($rst);
    }


    public function changeDel()
    {
        $classify_id   = $this->_param['id'];
        if (!$classify_id)
        {
            return $this->error('classify_id is required');
        }
        $input = $this->_input;
        $model = new ArticleClassifyModel();

        if ($input['status'] != 1 && ArticleModel::where([['classify_id', '=', $classify_id], ['status', '=', 1] ])->count())
        {
            return $this->error('this classify has article');
        }


        $rst = $model->updateByfilter(['id' => $classify_id], ['status' => $input['status']]);
        return $this->success($rst);
    }
}